class Solution(object):
    def minOperations(self, nums1, nums2):
        n1 = len(nums1)
        n2 = len(nums2)
        if n1 > 6 * n2 or 6 * n1 < n2:
            return -1
        t1 = sum(nums1)
        t2 = sum(nums2)
        if t1 == t2:
            return 0
        if t1 > t2:
            nums1, nums2 = nums2, nums1
            n1, n2 = n2, n1
            t1, t2 = t2, t1
        diff = t2 - t1
        nums1.sort()
        nums2.sort(reverse=True)
        left = 0
        right = 0
        ans = 0
        while left < n1 and right < n2:
            if 6 - nums1[left] >= nums2[right] - 1:
                diff -= (6 - nums1[left])
                ans += 1
                left += 1
            else:
                diff -= (nums2[right] - 1)
                ans += 1
                right += 1
            if diff <= 0:
                return ans
        while left < n1:
            diff -= (6 - nums1[left])
            left += 1
            ans += 1
            if diff <= 0:
                return ans
        while right < n2:
            diff -= (nums2[right] - 1)
            right += 1
            ans += 1
            if diff <= 0:
                return ans


data = Solution()
nums1 = [5, 6, 4, 3, 1, 2]
nums2 = [6, 3, 3, 1, 4, 5, 3, 4, 1, 3, 4]
print(data.minOperations(nums1, nums2))
